package com.lucy.property.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.ResponseDTO;
import com.lucy.property.entity.BuildingUnit;
import com.lucy.property.entity.PropertyBuilding;
import com.lucy.property.entity.UnitHouse;
import com.lucy.property.params.UnitHouseCreateParams;
import com.lucy.property.params.UnitHouseQueryParams;
import com.lucy.property.params.UnitHouseUpdateParams;
import com.lucy.property.service.BuildingUnitService;
import com.lucy.property.service.UnitHouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("unit")
@Api(tags = "单元房屋管理")
public class UnitHouseController {
    @Autowired
    UnitHouseService service;

    @Autowired
    BuildingUnitService unitService;

    @PostMapping("houses")
    @ApiOperation(value = "房屋新建",notes = "")
    public ResponseDTO<Object> houseCreate(@RequestBody @Validated UnitHouseCreateParams params){
        UnitHouse house = new UnitHouse();
        // 获取对应的楼栋信息
        Optional<BuildingUnit> unitOp = unitService.getOne(params.getBuildingUnitId());
        if (!unitOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND,"单元信息不存在");
        }
        BuildingUnit unit = unitOp.get();
        house.setBuildingUnitName(unit.getBuildingUnitName());
        BeanUtil.copyProperties(params,house, CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
        return ResponseDTO.succData(
                service.saveOne(house)
        );
    }

    @DeleteMapping("houses/{houseId}")
    @ApiOperation(value = "房屋删除",notes = "")
    public ResponseDTO<Object> houseDelete(@PathVariable("houseId") String houseId){
        service.deleteOne(houseId);
        return ResponseDTO.succMsg("删除成功");
    }

    @PutMapping("houses")
    @ApiOperation(value = "房屋修改",notes = "")
    public ResponseDTO<Object> houseUpdate(@RequestBody @Validated UnitHouseUpdateParams params){
        String houseId = params.getId();
        Optional<UnitHouse> houseOp = service.getOne(houseId);
        if (!houseOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        }
        UnitHouse house = houseOp.get();
        BeanUtil.copyProperties(params,house,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return ResponseDTO.succData(
                service.updateOne(house)
        );
    }

    @GetMapping("houses")
    @ApiOperation(value = "房屋列表",notes = "")
    public ResponseDTO<Object> houseListQuery(UnitHouseQueryParams params){
        Sort sort;
        if("DESC".equalsIgnoreCase(params.getOrder())){
            sort = Sort.by(Sort.Direction.DESC, params.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, params.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(params.getPageNum() - 1 , params.getPageSize(), sort);
        /*
        构建 Example
         */
        UnitHouse house = new UnitHouse();
        BeanUtil.copyProperties(params,house,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<UnitHouse> example = Example.of(house);
        return ResponseDTO.succData(service.getAll(pageable,example));
    }

    @GetMapping("houses/{houseId}")
    @ApiOperation(value = "房屋查询",notes = "")
    public ResponseDTO<Object> houseQuery(@PathVariable("houseId") String houseId){
        Optional<UnitHouse> entityOp = service.getOne(houseId);
        return entityOp.<ResponseDTO<Object>>map(ResponseDTO::succData).orElseGet(() -> ResponseDTO.wrap(HttpStatus.NOT_FOUND));
    }
}
